package com.qzimp.fujiancuo.controller;

import com.qzimp.fujiancuo.entity.Collection;
import com.qzimp.fujiancuo.entity.UserinfoLog;
import com.qzimp.fujiancuo.entity.vo.*;
import com.qzimp.fujiancuo.exceptions.ApiException;
import com.qzimp.fujiancuo.service.CollectionService;
import com.qzimp.fujiancuo.service.CuoService;
import com.qzimp.fujiancuo.service.UserinfoLogService;
import com.qzimp.fujiancuo.service.UserinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Api("接口")
@RequestMapping("/Liaohanping")
public class LiaohanpingController {
    @Autowired
    private CollectionService collectionService;
    @Autowired
    private UserinfoLogService userinfoLogService;
    @Autowired
    private UserinfoService userinfoService;
    @Autowired
    private CuoService cuoService;
    @PostMapping("save")
    @ApiOperation("添加收藏夹")
    public ResultVo save(@RequestBody @ApiParam("") Collection collection) {
        UserCollectionVo userCollectionVo = new UserCollectionVo();
        if (userinfoService.getById(collection.getUserinfoId())==null){
            return new  ResultVo(ResultCode.USERINFO_ERROR);
        }
        if (cuoService.getById(collection.getCuoId())==null){
            return new  ResultVo(ResultCode.CUO_ERROR);
        }else{
            collectionService.save(collection);
            userCollectionVo.setUserinfon(userinfoService.getById(collection.getUserinfoId()));
            userCollectionVo.setCollectionList(collectionService.getByUserId(collection.getUserinfoId()));;
        }
        return new ResultVo(ResultCode.SUCCESS,userCollectionVo);
    }
    @GetMapping("select")
    @ApiOperation(value = "通过名字查询用户操作的日志", notes = "根据用户Name获取事件")
    public UserHaveLogsVo selectByUserName(String username) {
        UserHaveLogsVo userHaveLogsVo = new UserHaveLogsVo();
        userHaveLogsVo.setLoginName(username);
        int id =  userinfoLogService.getUserIdByName(username);
        userHaveLogsVo.setUserinfoLogList(userinfoLogService.getUserinfoLogs(id));
        return userHaveLogsVo;
    }
}
